#include "btstack_config.h"

#include "hci_dump.h"
#include "hci.h"
#include "hci_transport.h"
#include "hci_cmd.h"
#include "btstack_run_loop.h"

#include "vsf.h"

#include <stdarg.h>
#include <stdio.h>

#if defined(ENABLE_LOG_INFO) || defined(ENABLE_LOG_ERROR) || defined(ENABLE_LOG_DEBUG)
static void printf_timestamp(void)
{
	uint32_t time_ms = btstack_run_loop_get_time_ms();
	vsfdbg_printf("[%04ums] ", time_ms);
}

static void printf_packet(uint8_t packet_type, uint8_t in, uint8_t * packet, uint16_t len)
{
	switch (packet_type)
	{
	case HCI_COMMAND_DATA_PACKET:
		vsfdbg_prints("CMD => " VSFCFG_DEBUG_LINEEND);
		break;
	case HCI_EVENT_PACKET:
		vsfdbg_prints("EVT <= " VSFCFG_DEBUG_LINEEND);
		break;
	case HCI_ACL_DATA_PACKET:
		if (in) {
			vsfdbg_prints("ACL <= " VSFCFG_DEBUG_LINEEND);
		} else {
			vsfdbg_prints("ACL => " VSFCFG_DEBUG_LINEEND);
		}
		break;
	case HCI_SCO_DATA_PACKET:
		if (in) {
			vsfdbg_prints("SCO <= " VSFCFG_DEBUG_LINEEND);
		} else {
			vsfdbg_prints("SCO => " VSFCFG_DEBUG_LINEEND);
		}
		break;
	case LOG_MESSAGE_PACKET:
		vsfdbg_printf("LOG -- %s\n\r", (char*) packet);
		return;
	default:
		return;
	}
	vsfdbg_printb(packet, len, 1, 16, true, true);
}

void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len)
{
	printf_timestamp();
	printf_packet(packet_type, in, packet, len);
}

void hci_dump_log(int log_level, const char * format, ...)
{
	char log_message_buffer[256];

	va_list argptr;
	va_start(argptr, format);
	int len = vsnprintf(log_message_buffer, sizeof(log_message_buffer), format, argptr);
	va_end(argptr);

	hci_dump_packet(LOG_MESSAGE_PACKET, 0, (uint8_t*) log_message_buffer, len);
}
#else
void hci_dump_packet(uint8_t packet_type, uint8_t in, uint8_t *packet, uint16_t len)
{

}
#endif